package com.bike.controller.information;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bike.controller.BaseController;
import com.bike.entity.HhBusiness;
import com.bike.entity.HhDiningStyle;
import com.bike.service.impl.HhBusinessServiceImpl;
import com.bike.service.impl.HhDiningStyleServiceImpl;
import com.bike.util.PageData;

 
@Controller
@RequestMapping(value="/hhDiningStyle")
public class HhDiningStyleController extends BaseController {
	
		@Autowired
		private HhDiningStyleServiceImpl  hhDiningStyleService;
		@Autowired
		private HhBusinessServiceImpl  hhBusinessServiceImpl;
	
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toList")
		public String toList(){
			return "/information/diningStyle/hhDiningStyle_list";
		}
		
		/**
		 * 商家选择桌牌背景样式
		 * @param request
		 * @param response
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/toStyleList")
		public String toStyleList(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
			PageData pd = PageData.getToPageData(request);
			HhBusiness business=hhBusinessServiceImpl.selectByHhBusinessId(pd.getLong("businessId"));
			pd.put("business", business.getBusinessId());
			pd.put("diningBack", business.getDiningBack());
			List<HhDiningStyle> list = hhDiningStyleService.listAll(pd);
			pd.put("rows", list);
			model.addAttribute("pd", pd);
			return "/information/diningStyle/dining_style_list"; 
		}
		
		/**
		 * 修改商家样式
		 * @param request
		 * @param response
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/editStyle")
		@ResponseBody
		public PageData editStyle(HttpServletRequest request, HttpServletResponse response) throws Exception{
			PageData pd =   PageData.getToPageData(request);
			try{
					HhBusiness business=new HhBusiness();
					business.setBusinessId(pd.getLong("businessId"));
					business.setDiningBack(pd.getString("diningBack"));
					hhBusinessServiceImpl.updateBusiness(pd, business);
					return getSuccessPd();
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(e.toString(), e);
			} 
			return getErrorPd("失败");
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("list")
		@ResponseBody
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return hhDiningStyleService.selectHhDiningStyleList(pd);
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("listAll")
		@ResponseBody
		public PageData listAll(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return hhDiningStyleService.selectHhDiningStyleList(pd);
		}
		/**
		 * 跳转到编辑页面（新增和修改）
		 */
		@RequestMapping("toEdit")
		public String toEdit(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			model.addAttribute("pd", pd);
			if(pd.getLong("diningStyleId")>0){
				model.addAttribute("pd", hhDiningStyleService.selectByHhDiningStyleId(pd.getLong("diningStyleId")));
			} 
			return "/information/diningStyle/hhDiningStyle_edit";
		}
		/**
		 * 搜素
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("toSearch")
		public String toSearch(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			model.addAttribute("pd", pd);
			return "/information/diningStyle/hhDiningStyle_search";
		}
		/**
		 * 新增和修改 
		 */
		@RequestMapping("save")
		@ResponseBody
		 public Map<String,Object> save(HttpServletRequest request,HhDiningStyle hhDiningStyle){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				hhDiningStyleService.saveOrUpdate(pd,hhDiningStyle);
				return getSuccessMap();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		 }
		/**
		 * 删除
		 */
	 	@RequestMapping("delete")
		@ResponseBody
		 public Map<String,Object> delete(HttpServletRequest request){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				if(pd.getLong("diningStyleId")>0){
					hhDiningStyleService.deleteHhDiningStyleById(pd.getLong("diningStyleId"));
					return getSuccessMap();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		}
}
